package com.xyhuo.leetcode.december;

import java.util.ArrayList;
import java.util.List;

/**
 * 矩阵置零
 * https://leetcode.cn/problems/set-matrix-zeroes/description/
 *
 * @time 2023/12/27
 */
public class SetZeroes {

//    public static void main(String[] args) {
//        SetZeroes sz = new SetZeroes();
//        int[][] matrix = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
//        sz.setZeroes(matrix);
//    }

    public void setZeroes(int[][] matrix) {
        this.m = matrix.length;
        this.n = matrix[0].length;

        List<Integer> xList = new ArrayList<>();
        List<Integer> yList = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            boolean findZero = false;
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == 0) {
                    findZero = true;
                    break;
                }
            }
            if (findZero) {
                xList.add(i);
            }
        }

        for (int i = 0; i < n; i++) {
            boolean findZero = false;
            for (int j = 0; j < m; j++) {
                if (matrix[j][i] == 0) {
                    findZero = true;
                    break;
                }
            }
            if (findZero) {
                yList.add(i);
            }
        }

        for (Integer index : xList) {
            zeroX(matrix, index);
        }
        for (Integer index : yList) {
            zeroY(matrix, index);
        }

    }

    private int m;
    private int n;

    private void zeroX(int[][] matrix, int index) {
        for (int i = 0; i < n; i++) {
            matrix[index][i] = 0;
        }
    }

    private void zeroY(int[][] matrix, int index) {
        for (int i = 0; i < m; i++) {
            matrix[i][index] = 0;
        }
    }
}
